import requests
import json

url = "http://beetle.amh-group.com/beetle/cargo_name/cargo_name_identify"#"http://beetle.amh-group.com/beetle/cargo_name/cargo_name_identify"#"http://127.0.0.1:8080/beetle/cargo_name/cargo_name_identify"
def httpPost(cargo_name,url):

    post_body = {
        "cargoName":cargo_name,
        "Transfer":1
    }
    headers = {
        'Content-Type':"application/json"
    }

    req = requests.post(url,data=json.dumps(post_body),headers=headers)
    if 'data' in req.json() and 'cargoNameInfo' in req.json()['data']:
        return req.json()['data']['cargoNameInfo']
    else:
        return {}

def checkJsonSame(jsonA,jsonB):
    same = True
    for key,value in jsonA.items():
        if key not in jsonB:
            same = False
        elif jsonB[key] != jsonA[key]:
            same = False

    return same


def main():
    testCast={
        #  未匹配到文本
        "hehehehe":{"cargoName": "","type": "0","badCaseName": ""},

        #  空字符串
        "": {"cargoName": "", "type": "1.csv", "badCaseName": ""},

        # 全词提取
        "我想运加气砖": {"cargoName": "加气砖", "type": "2", "badCaseName": ""},

        #相似纠错
        #相似纠错 -拼音纠错
        "我想运平果":{"cargoName": "苹果","type": "3","badCaseName": ""},
        # 相似纠错 -编辑距离纠错
        "树苗超长": {"cargoName": "树苗", "type": "3", "badCaseName": "超长"},
        "老婆": {"cargoName": "老婆", "type": "3", "badCaseName": ""},
        "通心菜": {"cargoName": "心菜", "type": "3", "badCaseName": ""},

        # 完全匹配
        "小青菜": {"cargoName": "小青菜", "type": "5", "badCaseName": ""},

        #命中屏蔽词
        "厂家直发": {"cargoName": "", "type": "6", "badCaseName": "厂家直发"},
    }

    for key,check_son in testCast.items():
        predictResult = httpPost(key,url)
        if (checkJsonSame(check_son,predictResult)):
            print(key+"测试ok")
        else:
            print(key+"测试不通过")


if __name__ == '__main__':
    main()

